package fanse;

import java.lang.reflect.*;

public class Test {
    public static void main(String[] args){
        try {
            Class<Student> clazz =Student.class;

            Student student = clazz.newInstance(); //创建对象
            Field name = clazz.getDeclaredField("name");
            name.setAccessible(true); //针对private属性
            name.set(student,"555");
            System.out.println(student);
            System.out.println("---------以上为调用private属性的-------------");
            Field field = clazz.getDeclaredField("age");//获取属性
            field.set(student,12);  //使用属性
            System.out.println(student);
            System.out.println("--------以上为调用public属性的--------------");

            Method show1 = clazz.getMethod("show");//获取方法无参方法
            Class returnType = show1.getReturnType();//获取方法的返回值类型
            show1.invoke(student);//使用方法
            Method show2 = clazz.getMethod("show",int.class,int.class);//获取方法有参方法
            Class[] parameterTypes = show2.getParameterTypes();//获取参数列表类型
            for (int i = 0; i < parameterTypes.length; i++) {
                System.out.println(parameterTypes[i]+"-args-"+(i+1));
            }
            show2.invoke(student,18,20);
            Class<?>[] exceptionTypes = show2.getExceptionTypes(); //获取异常类型
            for (int i = 0; i < exceptionTypes.length; i++) {
                System.out.print(show2.getName()+"  throws  ");
                System.out.println(exceptionTypes[i].getName()+" ");
            }
            Constructor<?>[] declaredConstructors = clazz.getDeclaredConstructors(); //获取所有构造方法
            for (Constructor<?> constructor : declaredConstructors) {
                System.out.println(constructor);
            }
           /* Type genericSuperclass = clazz.getGenericSuperclass(); //获取父类(带泛型）
            System.out.println(genericSuperclass);
            ParameterizedType superclass = null;
            try {
                superclass = (ParameterizedType) genericSuperclass; //只获取父类泛型
            } catch (Exception e) {
                e.printStackTrace();
            }
            Type[] actualTypeArguments = new Type[0];
            try {
                actualTypeArguments = superclass.getActualTypeArguments();
            } catch (Exception e) {
                e.printStackTrace();
            }
            for (Type type : actualTypeArguments) {
                System.out.println(type);
            }*/
            System.out.println("--------------第二种方式----------------------");
            show();

        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println("------------------------创建数组-------------------------");
        Object arr = Array.newInstance(String.class, 10);
        Array.set(arr,5,"55");
        Object o = Array.get(arr, 5);
        System.out.println(o);

    }
    public static void show() throws Exception {
        String str = "fanse.Student";
        Class clazz = Class.forName(str);
        Field[] Fields = clazz.getDeclaredFields(); //获取所有属性，getFields();只能获取运行时类中父类及其public修饰的
        for (Field field : Fields) {
            int i = field.getModifiers();
          //  System.out.println(Modifier.toString(i));   //1、可以获取修饰符
            Class type = field.getType();
            System.out.println(type.getName()+"");      // 2、属性类型

            //   System.out.println(field.getName());   //  3、属性名

        }
        Method[] methods = clazz.getDeclaredMethods();  //获取所有方法.获取运行时类本身声明的所有属性
        //创建对应的运行时类的对象
        Student student = (Student)clazz.newInstance();
       // System.out.println(student);
    }
}
